import { createStore } from 'vuex'
import router from '@/router'
import {ref} from "vue";

export default createStore({
  state: {
    token : null,
    userinfo : null,
    menuList : null,
    hasRoute : false,
    editableTabsValue : '/index',
    editableTabs : [
      {
        title: '首页',
        name: '/index',
      }
    ]
  },
  getters: {
    GET_TOKEN: state => {
      if (state.token == null) {
        state.token = sessionStorage.getItem("token")
      }

      return state.token
    },
    GET_USERINFO : state => {
      if (state.userinfo == null) {
        state.userinfo = JSON.parse(sessionStorage.getItem("userinfo"))
      }

      return JSON.parse(sessionStorage.getItem("userinfo"))
    },
    GET_MENULIST : state => {
      if (state.menuList == null) {
        state.menuList = JSON.parse(sessionStorage.getItem("menuList"))
      }
      return JSON.parse(sessionStorage.getItem("menuList"))
    }
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
      sessionStorage.setItem("token", token)
    },
    SET_UERINFO : (state, userinfo) => {
      state.userinfo = userinfo
      sessionStorage.setItem("userinfo", JSON.stringify(userinfo))
    },
    SET_MENULIST : (state, menuList) => {
      state.menuList = menuList
      sessionStorage.setItem("menuList", JSON.stringify(menuList))
    },
    SET_ROUTER_STATE: (state, hasRoute)=>{
      state.hasRoute = hasRoute;
    },
    ADD_TABS: (state, tab)=>{
      if (state.editableTabs.findIndex(e=> e.name === tab.mPath) === -1) {
        state.editableTabs.push({
          title: tab.mName,
          name: tab.mPath
        })
      }
      state.editableTabsValue = tab.mPath;
    },
    RESET_TABS: (state)=>{
      state.editableTabsValue = '/index',
      state.editableTabs = [
        {
          title: '首页',
          name: '/index',
        }
      ]
    }
  },

  actions: {
    logout(){
      window.sessionStorage.clear();
      router.replace("/login")
    }
  },
  modules: {
  }
})
